/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package co.baiku.boot.action;

import co.baiku.boot.common.tools.StringTools;
import co.baiku.boot.pay.Alipay;

import java.util.Calendar;

/**
 * 消息构造工具
 *
 * @author baoxing.gbx
 * @version $Id: AlipayMsgBuildUtil.java, v 0.1 Jul 24, 2014 5:47:19 PM baoxing.gbx Exp $
 */
public class AlipayMsgBuild {
    private Alipay alipay;

    public AlipayMsgBuild(Alipay alipay) {
        this.alipay = alipay;
    }

    /**
     * 构造单发图文消息
     *
     * @param alipayActionMessage
     * @return
     */
    public String buildSingleImgTextMsg(AlipayActionMessage alipayActionMessage) {
        return buildMsg(alipayActionMessage).toString();
    }

    public StringBuilder buildMsg(AlipayActionMessage alipayActionMessage) {
        StringBuilder sb = new StringBuilder();
        //构建json格式单发图文消息: 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'articles':[{'action_name':'").append(alipayActionMessage.getActionName());
        if (StringTools.isNotBlank(alipayActionMessage.getDesc())) {
            sb.append("','desc':'").append(alipayActionMessage.getDesc());
        }
        if (StringTools.isNotBlank(alipayActionMessage.getImageUrl())) {
            sb.append("','image_url':'").append(alipayActionMessage.getImageUrl());
        }
        if (StringTools.isNotBlank(alipayActionMessage.getTitle())) {
            sb.append("','title':'").append(alipayActionMessage.getTitle());
        }
        if (StringTools.isNotBlank(alipayActionMessage.getUrl())) {
            sb.append("','url':'").append(alipayActionMessage.getUrl());
        }
        sb.append("'}],'msg_type':'image-text'");
        if (StringTools.isNotBlank(alipayActionMessage.getUserId())) {
            sb.append(",'to_user_id':'").append(alipayActionMessage.getUserId());
        }
        sb.append("'}");
        return sb;
    }

    /**
     * 构造群发图文消息,不需要传递userId
     *
     * @return
     */
    public String buildGroupImgTextMsg(AlipayActionMessage alipayActionMessage) {
        alipayActionMessage.setUserId(null);
        return buildMsg(alipayActionMessage).toString();
    }

    /**
     * 构造单发纯文本消息
     *
     * @return
     */
    public String buildSingleTextMsg(AlipayActionMessage alipayActionMessage) {
        String sb = "{'msg_type':'text','text':{'content':'" +
                alipayActionMessage.getContent() + "'}, 'to_user_id':'" +
                alipayActionMessage.getUserId() + "'}";
        return sb;
    }

    /**
     * 构造群发纯文本消息
     *
     * @return
     */
    public String buildGroupTextMsg(String text) {
        //构建json格式群发纯文本消息体： 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        return "{'msg_type':'text','text':{'content':'" + text + "'}}";
    }

    /**
     * 构造基础的响应消息
     *
     * @return
     */
    public String buildBaseAckMsg(String userId) {
        String sb = "<XML>" +
                "<ToUserId><![CDATA[" + userId + "]]></ToUserId>" +
                "<AppId><![CDATA[" + alipay.getAlipayConfig().getAppid() + "]]></AppId>" +
                "<CreateTime>" + Calendar.getInstance().getTimeInMillis() + "</CreateTime>" +
                "<MsgType><![CDATA[ack]]></MsgType>" +
                "</XML>";
        return sb;
    }

}
